package com.work.tqyb.ui.activity;

import android.app.Activity;
import android.content.Intent;

import androidx.lifecycle.ViewModelProvider;

import com.work.tqyb.base.BaseActivity;
import com.work.tqyb.databinding.ActivityAirCityListBinding;
import com.work.tqyb.http.response.AirCityResponse;
import com.work.tqyb.http.response.AirProvincesResponse;
import com.work.tqyb.ui.adapter.AirCityAdapter;
import com.work.tqyb.ui.adapter.AirProvincesAdapter;
import com.work.tqyb.vm.AirVM;

public class AirCityListActivity extends BaseActivity {

    private AirVM vm;
    private ActivityAirCityListBinding binding;
    private AirProvincesAdapter adapter;
    private AirCityAdapter cityAdapter;

    @Override
    public void initData() {
        vm = new ViewModelProvider(this).get(AirVM.class);
        binding = ActivityAirCityListBinding.inflate(getLayoutInflater());
        adapter = new AirProvincesAdapter();
        cityAdapter = new AirCityAdapter();
        binding.lv.setAdapter(adapter);
        binding.lvCity.setAdapter(cityAdapter);
        setContentView(binding.getRoot());
        try {
            vm.airProvinces();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onViewClick() {
        binding.ibnBack.setOnClickListener(v -> finish());
        binding.lv.setOnItemClickListener((parent, view, position, id) -> {
            AirProvincesResponse.ProvincesInfo city = adapter.cityList.get(position);
            adapter.choiceInfo = city;
            adapter.notifyDataSetChanged();
            vm.airCitys(city.Id);
        });

        binding.lvCity.setOnItemClickListener((parent, view, position, id) -> {

            AirCityResponse.CityInfo city = cityAdapter.cityList.get(position);
            Intent intent = new Intent();
            intent.putExtra("city", city);
            setResult(Activity.RESULT_OK, intent);
            finish();

        });


    }

    @Override
    public void observe() {
        vm.getAirProvincesLD().observe(this, cityBeanArrayList -> {
            adapter.cityList = cityBeanArrayList;
            adapter.notifyDataSetChanged();
        });

        vm.getAirCityLD().observe(this, cityBeanArrayList -> {
            cityAdapter.cityList = cityBeanArrayList;
            cityAdapter.notifyDataSetChanged();
        });
    }
}
